package blog.com.mw.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import blog.com.mw.base.ResultData;
import blog.com.mw.pojo.vo.ArticleVO;
import blog.com.mw.service.ArticleService;

@RestController
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private ArticleService service;
	
	
	@RequestMapping(value = "/articleList", method = RequestMethod.GET)
	public ResultData getAllArticle(@RequestParam("page") int pageNum,@RequestParam("limit") int pageSize
			,@RequestParam(value="keyword", required=false)String keyword) {
		ResultData rs = null;
		rs = service.getAllArticle( pageNum, pageSize,keyword);
		return rs;
	}
	
	@RequestMapping(value = "/article", method = RequestMethod.GET)
	public ResultData getArticleById(@RequestParam("id") int id) {
		ResultData rs = null;
		rs = service.getArticleById(id);
		return rs;
	}
	
	@RequestMapping(value = "/getArticleByUserId", method = RequestMethod.GET)
	public ResultData getArticleByUserId(@RequestParam("userId") int userId,
			@RequestParam("page") int pageNum,@RequestParam("limit") int pageSize) {
		ResultData rs = null;
		rs = service.getArticleByUserId(userId,pageNum,pageSize);
		return rs;
	}
	
	@RequestMapping(value = "/getArticleByContent", method = RequestMethod.GET)
	public ResultData getArticleByContent(@RequestParam("id") int id,@RequestParam("isLabel") Boolean isLabel,
			@RequestParam("page") int pageNum,@RequestParam("limit") int pageSize) {
		ResultData rs = null;
		rs = service.getArticleByContent(id, isLabel,pageNum,pageSize);
		return rs;
	}
	
	@RequestMapping(value = "/article", method = RequestMethod.POST)
	public ResultData saveArticle(@RequestBody ArticleVO article) {
		ResultData rs = null;
		rs = service.saveArticle(article);
		return rs;
	}
	
	@RequestMapping(value = "/article/{id}", method = RequestMethod.DELETE)
	public ResultData delArticle(@PathVariable(value = "id") int id) {
		ResultData rs = null;
		List<Integer> ids = new ArrayList<>();
		ids.add(id);
		rs = service.delArticle(ids);
		return rs;
	}
	
	@RequestMapping(value = "/article", method = RequestMethod.DELETE)
	public ResultData delArticleByIds(@RequestBody List<Integer> ids) {
		ResultData rs = null;
		rs = service.delArticle(ids);
		return rs;
	}
}
